package com.lianzhihui.romeex.ui.orepool;

import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.MyTeamActivityAdapter;
import com.lianzhihui.romeex.bean.MyTeamBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.MyTeamPresnterImp;
import com.lianzhihui.romeex.mvp.view.MyTeamView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.me.InvitationActivity;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.NumberUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.BreakIterator;
import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.orepool
 * @ClassName: MyTeamActivity
 * @Description: 我的团队
 * @Author: zhanghong
 * @CreateDate: 2020/11/24 15:02
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/24 15:02
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MyTeamActivity extends BaseActivity implements MyTeamView, OnRefreshListener {
    @BindView(R.id.myRecyclerView)
    ZFEmptyRecyclerView myRecyclerView;

    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.tv_paidan_num)
    TextView tv_paidan_num;
    @BindView(R.id.tv_fuxing_num)
    TextView tv_fuxing_num;
    @BindView(R.id.my_level)
    TextView my_level;
    @BindView(R.id.status_bar_id)
    View statusBarId;
    @BindView(R.id.directCount)
    TextView directCount;
    @BindView(R.id.teamCount)
    TextView teamCount;
    @BindView(R.id.teamValidCount)
    TextView teamValidCount;
    @BindView(R.id.tv_community1)
    TextView tvCommunity1;
    @BindView(R.id.tv_community2)
    TextView tvCommunity2;
    @BindView(R.id.tv_community3)
    TextView tvCommunity3;
    @BindView(R.id.tv_community4)
    TextView tvCommunity4;
    @BindView(R.id.tv_community5)
    TextView tvCommunity5;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    private MyTeamActivityAdapter myTeamActivityAdapter;
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.my_level_icon)
    ImageView my_level_icon;
    private MyTeamPresnterImp myTeamPresnterImp;
    private int communityLevel = -1;
    private String inviteCode = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_team;

    }

    @Override
    public boolean setFitSystemWindowFlag() {
        return false;
    }

    @Override
    protected void initView() {
        mySmartRefreshLayout.setOnRefreshListener(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarId.getHeight());
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(MyApplication.mContext);
        statusBarId.setLayoutParams(layoutParams);
        includeTopTvTitle.setTextColor(getResources().getColor(R.color.white));
        includeTopTvTitle.setText(getResources().getText(R.string.text_my_team));
        StatusBarUtil.setStatusBarTextColor(this, false);
        StatusBarUtil.setTransparentForImageView(this, null);
        includeTopIvBack.setImageResource(R.drawable.icon_return_w);
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.transparent));
        myTeamPresnterImp = new MyTeamPresnterImp(this, this);

    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        myTeamActivityAdapter = new MyTeamActivityAdapter(this, new ArrayList<>());
        myRecyclerView.setAdapter(myTeamActivityAdapter);
//        if (null != extras) {
//            MyTeamBean teamBean = (MyTeamBean) extras.get(AppConstants.TEAM_BEAN);
//            setNewData(teamBean);
//        } else {
        mySmartRefreshLayout.autoRefresh();
//        }

    }

    @OnClick({R.id.tv_earnings, R.id.tv_rule, R.id.tv_inv, R.id.count})
    public void onClick(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (view.getId()) {
                case R.id.tv_earnings:
                    IntentUtils.JumpTo(EarningsActivity.class);
                    break;
                case R.id.count:
                    IntentUtils.JumpTo(EarningsActivity.class);
                    break;
                case R.id.tv_rule:
                    if (communityLevel != -1) {
                        IntentUtils.JumpToHaveOneInt(CommunityRuleActivity.class, AppConstants.USERLEVEL, communityLevel);
                    }
                    break;
                case R.id.tv_inv:
                    //邀请好友
                    if (!inviteCode.isEmpty()) {
                        IntentUtils.JumpToHaveOne(InvitationActivity.class, AppConstants.INTENT_INVITATION_CODE, inviteCode);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void findSunRelationSuccess(MyTeamBean bean) {
        setNewData(bean);
        mySmartRefreshLayout.finishRefresh();
    }


    private void setNewData(MyTeamBean bean) {
        if (bean == null) {
            return;
        }
        inviteCode = bean.getInviteCode();
        //团队复兴值
        if (bean.getSunTop().getTeamFx() != null && !bean.getSunTop().getTeamFx().isEmpty()) {
            tv_fuxing_num.setText(MessageFormat.format("{0}R", NumberUtils.big8(Double.parseDouble(bean.getSunTop().getTeamFx()))));
        }

        //邀请总收益
        count.setText(NumberUtils.big8(Double.parseDouble(bean.getSunTop().getInviteTotalBalance())));
        //排单复兴值
        if (bean.getSunTop().getTeamPd() != null && !bean.getSunTop().getTeamPd().isEmpty()) {
            tv_paidan_num.setText(MessageFormat.format("{0}R", NumberUtils.big8(Double.parseDouble(bean.getSunTop().getTeamPd()))));
        }
        //我的社区等级
        communityLevel = bean.getCommunityInfo().getCommunityLevel();
        if (communityLevel == 0) {
            my_level.setText(MessageFormat.format("{0}{1}", getResources().getString(R.string.text_my_community_level), "无"));
        } else {

            my_level.setText(MessageFormat.format("{0}R{1}", getResources().getString(R.string.text_my_community_level), communityLevel));
        }
        //直推人数
        directCount.setText(String.valueOf(bean.getChildInfo().getDirectCount()));
        //团队人数
        teamCount.setText(String.valueOf(bean.getChildInfo().getTeamCount()));
        //团队有效用户
        teamValidCount.setText(String.valueOf(bean.getChildInfo().getTeamValidCount()));
        tvCommunity1.setText(String.valueOf(bean.getCommunityInfo().getCommunityChild().get(0).getCount()));
        tvCommunity2.setText(String.valueOf(bean.getCommunityInfo().getCommunityChild().get(1).getCount()));
        tvCommunity3.setText(String.valueOf(bean.getCommunityInfo().getCommunityChild().get(2).getCount()));
        tvCommunity4.setText(String.valueOf(bean.getCommunityInfo().getCommunityChild().get(3).getCount()));
        tvCommunity5.setText(String.valueOf(bean.getCommunityInfo().getCommunityChild().get(4).getCount()));

        setCommunityData(tvCommunity1, bean.getCommunityInfo().getCommunityChild().get(0).getCommunityLevel());
        setCommunityData(tvCommunity2, bean.getCommunityInfo().getCommunityChild().get(1).getCommunityLevel());
        setCommunityData(tvCommunity3, bean.getCommunityInfo().getCommunityChild().get(2).getCommunityLevel());
        setCommunityData(tvCommunity4, bean.getCommunityInfo().getCommunityChild().get(3).getCommunityLevel());
        setCommunityData(tvCommunity5, bean.getCommunityInfo().getCommunityChild().get(4).getCommunityLevel());
        //设置我的等级图标
        if (bean.getSunTop().getMyLevel() != 0) {
            setMyLevelData(my_level_icon, bean.getSunTop().getMyLevel());
        } else {
            my_level_icon.setVisibility(View.INVISIBLE);
        }

        myRecyclerView.showEmptyView(bean.getChildInfo().getDirectList().size(), 1);
        myTeamActivityAdapter.setNewData(bean.getChildInfo().getDirectList());

    }

    private void setCommunityData(TextView tv, int level) {
        Drawable drawable = getResources().getDrawable(R.drawable.icon_community1);
        switch (level) {
            case 1:
                drawable = getResources().getDrawable(R.drawable.icon_community1);
                break;
            case 2:
                drawable = getResources().getDrawable(R.drawable.icon_community2);
                break;
            case 3:
                drawable = getResources().getDrawable(R.drawable.icon_community3);
                break;
            case 4:
                drawable = getResources().getDrawable(R.drawable.icon_community4);
                break;
            case 5:
                drawable = getResources().getDrawable(R.drawable.icon_community5);
                break;
            default:

                break;
        }
        tv.setCompoundDrawablePadding(10);
        tv.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    }

    private void setMyLevelData(ImageView iv, int level) {
        Drawable drawable = getResources().getDrawable(R.drawable.icon_grade1_b);
        switch (level) {
            case 1:
                drawable = getResources().getDrawable(R.drawable.icon_grade1_b);
                break;
            case 2:
                drawable = getResources().getDrawable(R.drawable.icon_grade2_b);
                break;
            case 3:
                drawable = getResources().getDrawable(R.drawable.icon_grade3_b);
                break;
            case 4:
                drawable = getResources().getDrawable(R.drawable.icon_grade4_b);
                break;
            case 5:
                drawable = getResources().getDrawable(R.drawable.icon_grade5_b);
                break;
            case 6:
                drawable = getResources().getDrawable(R.drawable.icon_grade6_b);
                break;
            case 7:
                drawable = getResources().getDrawable(R.drawable.icon_grade7_b);
                break;
            case 8:
                drawable = getResources().getDrawable(R.drawable.icon_grade8_b);
                break;
            case 9:
                drawable = getResources().getDrawable(R.drawable.icon_grade9_b);
                break;
            case 10:
                drawable = getResources().getDrawable(R.drawable.icon_grade10_b);
                break;
            default:
                break;
        }
        iv.setImageDrawable(drawable);
    }

    @Override
    public void setFailure() {
        mySmartRefreshLayout.finishRefresh();
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        myTeamPresnterImp.requestSunFindsunrelation();
    }
}
